﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
using CourseArraging.Common; 
using CourseArraging.Frm.jygl;
using Stone.Library.Common;
using Stone.DataContracts;

namespace CourseArraging.Frm.jxgl
{
    /// <summary>
    /// 版权所有
    /// 石世特@温州医学院 sst330381@gmail.com
    /// </summary> 
    public partial class FrmLsap2 : FrmToolbarBase
    {
        #region 字段属性
        private FrmTecItem frmtecintem;
        private TecEntity tecEntity;
        private LsapEntity lsapentity;

        BasicService.BasicServiceClient courseDao = new BasicService.BasicServiceClient();
        LsapService.LsapServiceClient lsapdao = new LsapService.LsapServiceClient();

        public delegate void lsapDelegate();
        public event lsapDelegate lsapevent;
        #endregion

        #region 构造函数
        public FrmLsap2(LsapEntity lsapEntity)
        {
            InitializeComponent();
            this.lsapentity = lsapEntity;
            this.label_xyxm.Text = lsapEntity.lsap_xyxm;
            this.label_jyxm.Text = lsapEntity.lsap_jyxm;
            this.label_sksj.Text = lsapEntity.lsap_kcsj;
            this.txt_jyno.Click += new EventHandler(txt_jyno_Click);
            this.txt_jyno.KeyPress += new KeyPressEventHandler(txt_jyno_KeyPress);
            InintyCmbox();
        }

        #endregion

        #region 事件
        private void InintyCmbox()
        {
            DataTable dtCourses = courseDao.getCourseInfo4table(0,null);
            this.cmb_km.DataSource = dtCourses;
            this.cmb_km.DisplayMember = "课程";
            this.cmb_km.ValueMember = "编号";
        }

        void txt_jyno_Click(object sender, EventArgs e)
        {
            this.frmtecintem = new FrmTecItem(null, "T" + CommonHelper.course2simple(this.label_sksj.Text));
            //this.frmtecintem = new FrmTecItem(null, this.label_sksj.Text);
            this.frmtecintem.selectedOK += new UControl.FrmControlBase.SelectedItem(frmstuintem_selectedOK);
            this.frmtecintem.ShowDialog();
        }

        void frmstuintem_selectedOK(object entity)
        {
            if (entity != null)
            {
                this.tecEntity = (TecEntity)entity;
                this.txt_jyno.Text = this.tecEntity.user_no;
                this.label_jyxm.Text = this.tecEntity.user_name;
            }
        }

        void txt_jyno_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar == '\r') && (this.frmtecintem == null))
            {
                this.frmtecintem = new FrmTecItem(this.txt_jyno.Text, null);
                this.frmtecintem.selectedOK += new UControl.FrmControlBase.SelectedItem(frmstuintem_selectedOK);
                this.frmtecintem.ShowDialog();
            }
            else if ((e.KeyChar == '\r') && (this.frmtecintem != null))
            {
                this.frmtecintem.ShowData(this.txt_jyno.Text, null);
                this.frmtecintem.ShowDialog();
            }
        }

        #endregion

        #region 实现抽象方法

        public override void tsb_save_fun()
        {
            lsapentity.lsap_km = this.cmb_km.Text;
            lsapentity.lsap_jybh = txt_jyno.Text;
            lsapentity.lsap_jyxm = label_jyxm.Text;
            lsapentity.lsap_bz = rbox_bz.Text;
            lsapentity.lsap_zt = "正在授课";
            //lsapentity.lsap_bj=CommonHelper.course2simple(this.label_sksj.Text.Split
            //    (new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries)[0]);
            lsapdao.Add(lsapentity);
            if(lsapevent!=null)
                lsapevent();
            this.Close();
        }

        public override bool be4save()
        {
            if (!string.IsNullOrEmpty(this.label_jyxm.Text) &&
                !string.IsNullOrEmpty(this.cmb_km.Text))
                return true;
            else
                return false;
        }

        public override void tsb_search_fun()
        {
            FrmTecItem frm = new FrmTecItem(null, null);
            frm.ShowDialog();
        }

        #endregion
    }
}
